<?
	include("includes/config.php");
	include("includes/login.php");
	include("includes/functions.php");
	$nr_comanda = $_GET['nr_comanda'];
	$nr_inmatc = $_GET['nr_inmat'];
	$id_comanda = $_GET['id_comanda'];
	$id_utilizator = $_SESSION['id_utilizator'];
	if(@$_POST['add_articol']){
		
		$descriere = $_POST['desc'];
		$norma = $_POST['norma'];
		$cant = $_POST['cant'];
		$manopera = $_POST['manopera'];
		$manopera = explode("&&",$manopera);
		$pret_manopera = $manopera[0];
		$nume_manopera = $manopera[1];
		$tarif = $pret_manopera ;
		
		
		mysql_query("insert into
			comenzi_reparatii (
				id_reparatie,
				id_comanda,
				descriere,
				norma,
				cant,
				tip_manopera,
				tarif
			) values(
				NULL,
				'$id_comanda',
				'$descriere',
				'$norma',
				'$cant',
				'$nume_manopera',
				'$tarif'
			)") or die("query inser in materiale" .mysql_error());
		}
		
	if(@$_GET['stergeReparatie']){
		$id_reparatie = $_GET['stergeReparatie'];
		mysql_query("delete from comenzi_reparatii where id_reparatie = '$id_reparatie'");
	}


?>
<html>
	<head>
		<link rel="stylesheet" type="text/css" href="css/style.css" />
	</head>
	<body>
		<div id="header"><h1> <? echo detaliFirma('nume_firma'); ?></h1></div>
		<div id="warper">
			<?
				$resMeniu = mysql_query("select * from meniu");
			?>
			<div id="menu">
				<ul>
					<? while($rowMen = mysql_fetch_array($resMeniu)): ?>
					<li><a href="<?echo $rowMen['link'];?>"><? echo $rowMen['numeLink']; ?></a></li>
					<? endwhile; ?>
				</ul>
			</div>
			<div id="content">
				<div id="contentWrap">
					<div class="block" style="height:60px;">
						<div style="width:380px; float:left;height:60px;">
							<div style="width:380px;height:40px;" >
								<b style="color:#00a652;font-size:18px;">Comanda Deschisa Nr. <? echo detaliFirma('serie_factura'). $nr_comanda; ?></b>
							</div>
								<div style="width:385px;float:left">
								<a class="buton" href="/comandanoua.php?nr_comanda=<? echo $nr_comanda; ?>&id_client=1&id_masina=1" >Sumar </a>&nbsp;
								<a class="buton" href="/comandanoua_simptome_constatari.php?id_comanda=<? echo $id_comanda; ?>&nr_comanda=<? echo $nr_comanda; ?>&id_client=1&id_masina=1&nr_inmat=<? echo @$_GET['nr_inmat']; ?>" >Simptome / Constatari </a>&nbsp;
								<a class="buton" href="/comandanoua_materiale.php?id_comanda=<? echo $id_comanda; ?>&nr_comanda=<? echo $nr_comanda; ?>&id_client=1&id_masina=1&nr_inmat=<? echo @$_GET['nr_inmat']; ?>" >Materiale </a>&nbsp;
								<a class="buton"  href="/comandanoua_reparatii.php?id_comanda=<? echo $id_comanda; ?>&nr_comanda=<? echo $nr_comanda; ?>&id_client=1&id_masina=1&nr_inmat=<? echo @$_GET['nr_inmat']; ?>"  >Reparatii </a>&nbsp;
							</div>
						</div>
						<div style="width:200px;height:55px;background:#707070;float:left;margin-left:180px;padding:4px;">
							<?
								/**************************** MATERIALE QUERY **************************/
									$res_pret_materiale = mysql_query("select pret,cant from comenzi_materiale,comenzi_materiale_relatii where comenzi_materiale.id_material = comenzi_materiale_relatii.id_material and id_comanda = '$id_comanda' ");
									$pret_materiale = array();
									while($rowPret = mysql_fetch_array($res_pret_materiale)){
										$pret_materiale[] = $rowPret['pret'] * $rowPret['cant']  ;
									}
									$pret_materiale = array_sum($pret_materiale);
								/**************************** MATERIALE QUERY **************************/	
									
								/**************************** REPARATI QUERY **************************/
									$res_pret_reparatii = mysql_query("select tarif,cant from comenzi_reparatii where  id_comanda = '$id_comanda' ");
									$pret_reparatii = array();
									while($rowPret = mysql_fetch_array($res_pret_reparatii)){
										$pret_reparatii[] = $rowPret['tarif'] * $rowPret['cant']  ;
									}
									$pret_reparatii = array_sum($pret_reparatii);
								/**************************** REPARATI QUERY **************************/
							?>
							Total materiale cu TVA: <? echo $pret_materiale; ?> RON<br/>
							
							Total Reparatii cu TVA <? echo $pret_reparatii; ?> RON<br/>
							<hr>
							<b>Total <? echo $pret_materiale + $pret_reparatii  ?> RON</b>
						</div>
					</div>
					
					<h2>Reparatii necesare comenzii </h2>
					
					<table>
						<tr id="capTabel">
							<td style="width:400px; text-align:center;">Descriere</td>
							<td style="width:80px;  text-align:center;">Norma</td>
							<td style="width:80px; text-align:center;" >Tip Manopera </td>
							<td style="width:80px; text-align:center;">Tarif</td>
							<td style="width:80px; text-align:center;">Valoare totala</td>
							<td style="width:100px; text-align:center;"></td>
						</tr>
						<?
							$resM = mysql_query("select * from comenzi_reparatii where id_comanda = '$id_comanda'");
							while($rowMaterial = mysql_fetch_array($resM)):
						?>
						<tr style="height:30px;">
							<td style="width:30px;"><? echo $rowMaterial['descriere']; ?></td>
							<td style="width:120px; text-align:center;"><? echo $rowMaterial['norma']; ?></td>
							<td style="width:150px; text-align:center;"><? echo $rowMaterial['tip_manopera']; ?></td>
							<?
								$valoare_totala  = $rowMaterial['tarif'] * $rowMaterial['cant'] * $rowMaterial['norma'] ;
							?>
							<td style="width:80px;  text-align:center;"><? echo $rowMaterial['tarif']; ?></td>
							<td style="width:80px;  text-align:center;"><? echo $valoare_totala; ?></td>
							<td style="width:100px; text-align:center;"><a class="buton" href="/comandanoua_reparatii.php?stergeReparatie=<? echo $rowMaterial['id_reparatie']; ?>&id_comanda=<? echo $_GET['id_comanda'];?>&nr_comanda=<? echo $_GET['nr_comanda']; ?>&id_client=<? echo $_GET['id_client']; ?>&nr_inmat=<? echo $_GET['nr_inmat'];?> ">sterge </a></td>
						</tr>
						<? endwhile; ?>
					</table>
					<div class="block"></div>
					
					<form action="" method="post">
						<div style="margin-left:50px; width:350px;height:120px;float:left;">
						<p>
							<label>Descriere reparatie</label>
							<input class="text" type="text" name="desc"  id="descriere" value="">
						</p>
						<p>
							<label>Norma</label>
							<input class="text" type="text" name="norma" value="">
						</p>
						
						</div>
						<div style="width:350px;height:120px;float:left;">
						<p>
							<label>Cantitate</label>
							<input class="text" type="text" name="cant"  value="">
						</p>
						<p>
							<label>
								Tip Manopera
							</label>
							<?
								$resjobs = mysql_query("select * from  preturi where id_unitate = '$id_utilizator' ") or die(mysql_error());
							?>
							<select style="width:276px;height:26px;margin-top:5px;" name="manopera">
								<? while($job = mysql_fetch_array($resjobs)):?>
								<option value="<? echo $job['pret']; ?>&&<? echo $job['tip_manopera']?>"><? echo $job['tip_manopera']?></option>
								<? endwhile; ?>
							</select>
							
						</p>
						</div>
						<p>
						<input class="buton" style="margin-left:300px;margin-top:10px" type="submit" value="Adauga Reparatie" name="add_articol">
						</p>
					</form>
					<div class="block" style="clear:both;"></div>

				</div>
			</div>
		</div>
	
	</body>
	
</html>